///@Description(描述)     通用刷新组件封装
///@author(作者)          zhangyinhong
///@create(时间)          2025-01-20 15:00

import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:app_core/app_core.dart';

import '../common/constant/index.dart';

/// 通用刷新组件
class EasyRefreshWidget extends StatelessWidget {
  const EasyRefreshWidget({
    super.key,
    required this.child,
    this.enablePullToRefresh = false,
    this.enableLoadMore = false,
    this.onRefresh,
    this.onLoadMore,
    this.headerConfig,
    this.footerConfig,
  });

  /// 子组件
  final Widget child;

  /// 是否启用下拉刷新
  final bool enablePullToRefresh;

  /// 是否启用上拉加载更多
  final bool enableLoadMore;

  /// 下拉刷新回调
  final Future<void> Function()? onRefresh;

  /// 上拉加载更多回调
  final Future<void> Function()? onLoadMore;

  /// Header配置
  final EasyRefreshHeaderConfig? headerConfig;

  /// Footer配置
  final EasyRefreshFooterConfig? footerConfig;

  @override
  Widget build(BuildContext context) {
    if (!enablePullToRefresh && !enableLoadMore) {
      return child;
    }

    return _buildEasyRefresh(context);
  }

  /// 构建EasyRefresh组件
  Widget _buildEasyRefresh(BuildContext context) {
    return EasyRefresh(
      header: enablePullToRefresh ? _buildHeader(context) : null,
      footer: enableLoadMore ? _buildFooter(context) : null,
      onRefresh: enablePullToRefresh ? _handleRefresh : null,
      onLoad: enableLoadMore ? _handleLoadMore : null,
      child: child,
    );
  }

  /// 构建Header
  Header _buildHeader(BuildContext context) {
    final config = headerConfig ?? EasyRefreshHeaderConfig.defaultConfig();

    return ClassicHeader(
      dragText: config.dragText,
      armedText: config.armedText,
      readyText: config.readyText,
      processingText: config.processingText,
      processedText: config.processedText,
      noMoreText: config.noMoreText,
      failedText: config.failedText,
      messageText: config.messageText,
      showMessage: config.showMessage,
      textStyle: config.textStyle ?? (config.textColor != null ? TextStyle(color: config.textColor) : null),
      messageStyle: config.messageStyle ?? (config.messageColor != null ? TextStyle(color: config.messageColor) : null),
      iconTheme:
          config.iconSize != null || config.iconColor != null
              ? IconThemeData(size: config.iconSize ?? 18.0, color: config.iconColor ?? Theme.of(context).primaryColor)
              : null,
    );
  }

  /// 构建Footer
  Footer _buildFooter(BuildContext context) {
    final config = footerConfig ?? EasyRefreshFooterConfig.defaultConfig();

    return ClassicFooter(
      dragText: config.dragText,
      armedText: config.armedText,
      readyText: config.readyText,
      processingText: config.processingText,
      processedText: config.processedText,
      noMoreText: config.noMoreText,
      failedText: config.failedText,
      messageText: config.messageText,
      showMessage: config.showMessage,
      textStyle: config.textStyle ?? (config.textColor != null ? TextStyle(color: config.textColor) : null),
      messageStyle: config.messageStyle ?? (config.messageColor != null ? TextStyle(color: config.messageColor) : null),
      iconTheme:
          config.iconSize != null || config.iconColor != null
              ? IconThemeData(size: config.iconSize ?? 18.0, color: config.iconColor ?? Theme.of(context).primaryColor)
              : null,
    );
  }

  /// 处理下拉刷新
  Future<void> _handleRefresh() async {
    if (onRefresh == null) return;

    try {
      await onRefresh!();
    } catch (e) {
      YHLog.e('下拉刷新失败', error: e);
      rethrow;
    }
  }

  /// 处理上拉加载
  Future<void> _handleLoadMore() async {
    if (onLoadMore == null) return;

    try {
      await onLoadMore!();
    } catch (e) {
      YHLog.e('上拉加载失败', error: e);
      rethrow;
    }
  }
}

/// Header配置类
class EasyRefreshHeaderConfig {
  const EasyRefreshHeaderConfig({
    required this.dragText,
    required this.armedText,
    required this.readyText,
    required this.processingText,
    required this.processedText,
    required this.noMoreText,
    required this.failedText,
    required this.messageText,
    this.showMessage = false,
    this.textStyle,
    this.messageStyle,
    this.textColor,
    this.messageColor,
    this.iconSize,
    this.iconColor,
  });

  final String dragText;
  final String armedText;
  final String readyText;
  final String processingText;
  final String processedText;
  final String noMoreText;
  final String failedText;
  final String messageText;
  final bool showMessage;
  final TextStyle? textStyle;
  final TextStyle? messageStyle;
  final Color? textColor;
  final Color? messageColor;
  final double? iconSize;
  final Color? iconColor;

  /// 默认中文配置
  factory EasyRefreshHeaderConfig.defaultConfig() {
    return   EasyRefreshHeaderConfig(
      dragText: '下拉刷新',
      armedText: '释放刷新',
      readyText: '正在刷新...',
      processingText: '正在刷新...',
      processedText: '刷新完成',
      noMoreText: '没有更多了',
      failedText: '刷新失败',
      messageText: '最后更新于 %T',
      showMessage: true,
      // textColor: Colors.blue,        // 主文字颜色
      // messageColor: Colors.grey,     // 时间文字颜色
      iconColor: AppColor.primary,
      // iconSize: 16
    );
  }

  /// 英文配置
  factory EasyRefreshHeaderConfig.englishConfig() {
    return const EasyRefreshHeaderConfig(
      dragText: 'Pull to refresh',
      armedText: 'Release to refresh',
      readyText: 'Refreshing...',
      processingText: 'Refreshing...',
      processedText: 'Refresh completed',
      noMoreText: 'No more data',
      failedText: 'Refresh failed',
      messageText: 'Last updated at %T',
      showMessage: true,
    );
  }
}

/// Footer配置类
class EasyRefreshFooterConfig {
  const EasyRefreshFooterConfig({
    required this.dragText,
    required this.armedText,
    required this.readyText,
    required this.processingText,
    required this.processedText,
    required this.noMoreText,
    required this.failedText,
    required this.messageText,
    this.showMessage = false,
    this.textStyle,
    this.messageStyle,
    this.textColor,
    this.messageColor,
    this.iconSize,
    this.iconColor,
  });

  final String dragText;
  final String armedText;
  final String readyText;
  final String processingText;
  final String processedText;
  final String noMoreText;
  final String failedText;
  final String messageText;
  final bool showMessage;
  final TextStyle? textStyle;
  final TextStyle? messageStyle;
  final Color? textColor;
  final Color? messageColor;
  final double? iconSize;
  final Color? iconColor;

  /// 默认中文配置
  factory EasyRefreshFooterConfig.defaultConfig() {
    return const EasyRefreshFooterConfig(
      dragText: '上拉加载',
      armedText: '释放加载',
      readyText: '正在加载...',
      processingText: '正在加载...',
      processedText: '加载完成',
      noMoreText: '没有更多了',
      failedText: '加载失败',
      messageText: '最后更新于 %T',
      showMessage: true,
    );
  }

  /// 英文配置
  factory EasyRefreshFooterConfig.englishConfig() {
    return const EasyRefreshFooterConfig(
      dragText: 'Pull to load',
      armedText: 'Release to load',
      readyText: 'Loading...',
      processingText: 'Loading...',
      processedText: 'Load completed',
      noMoreText: 'No more data',
      failedText: 'Load failed',
      messageText: 'Last updated at %T',
      showMessage: true,
    );
  }
}

/// 快捷构造方法扩展
extension EasyRefreshWidgetExtension on Widget {
  /// 包装下拉刷新
  Widget wrapRefresh({
    bool enablePullToRefresh = true,
    bool enableLoadMore = false,
    Future<void> Function()? onRefresh,
    Future<void> Function()? onLoadMore,
    EasyRefreshHeaderConfig? headerConfig,
    EasyRefreshFooterConfig? footerConfig,
  }) {
    return EasyRefreshWidget(
      enablePullToRefresh: enablePullToRefresh,
      enableLoadMore: enableLoadMore,
      onRefresh: onRefresh,
      onLoadMore: onLoadMore,
      headerConfig: headerConfig,
      footerConfig: footerConfig,
      child: this,
    );
  }

  /// 快捷包装下拉刷新
  Widget wrapPullRefresh({Future<void> Function()? onRefresh}) {
    return wrapRefresh(enablePullToRefresh: true, enableLoadMore: false, onRefresh: onRefresh);
  }

  /// 快捷包装下拉刷新+上拉加载
  Widget wrapPullAndLoad({Future<void> Function()? onRefresh, Future<void> Function()? onLoadMore}) {
    return wrapRefresh(enablePullToRefresh: true, enableLoadMore: true, onRefresh: onRefresh, onLoadMore: onLoadMore);
  }
}
